package ac.github.os.internal.core.condition.impl.entity

import ac.github.os.internal.core.condition.Condition
import ac.github.os.internal.core.config.StringConfig
import ac.github.os.internal.core.config.get
import ac.github.os.internal.core.holder.EntityCompatible
import ac.github.os.internal.core.holder.player.PlayerInteractAdyEntityHolder
import ac.github.os.internal.session.Session
import taboolib.common.platform.Awake

abstract class EntityCondition(
    val name : String
) : Condition{
    override fun doScreen(session: Session, config: StringConfig): Boolean {
        val holder = session.holder
        if (holder is EntityCompatible<*>) {
            return run(session,config,holder)
        }
        return false
    }

    abstract fun run(session: Session, config: StringConfig, compatible: EntityCompatible<*>): Boolean

    fun StringConfig.value(def: String? = null): String? = get("val", def, single = true)
    override val namespace: String
        get() = "entity:$name"
}

@Awake
class InteractEntityType : EntityCondition("type") {
    override fun run(session: Session, config: StringConfig, compatible: EntityCompatible<*>): Boolean {
        return compatible.entity().type.name == config.value()
    }
}

@Awake
class InteractAdyEntityId : EntityCondition("ady") {
    override fun run(session: Session, config: StringConfig, compatible: EntityCompatible<*>): Boolean {
        return if (compatible is PlayerInteractAdyEntityHolder) {
            config.value()!! == compatible.event.entity.id
        } else false
    }
}



@Awake
class InteractEntityName : EntityCondition("name") {
    override fun run(session: Session, config: StringConfig, compatible: EntityCompatible<*>): Boolean {
        return config.value() == compatible.entity().displayName
    }
}